<?php
/**
 * ShopEx licence
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 */

class tags_ctl_admin_tags extends desktop_controller{
	var $url = 'index.php?app=tags&ctl=admin_tags';

    public function index(){
    	$url_params = '&wg='.urlencode($_GET['wg']).'&type='.urlencode($_GET['type']).'&app_id='.urlencode($_GET['app_id']);
        $params = array(
            'title'   => app::get('desktop')->_('标签管理'),
            'actions' => array(
                array(
                    'label' => app::get('desktop')->_('新建普通标签'),
                    'icon'  => 'fa fa-plus',
                    'href'  => $this->url . '&act=tag_add' . $url_params,'target'=>'dialog::{title:\''.app::get('desktop')->_('新建普通标签').'\'}'
                ),
            ),
            'base_filter'=>array(
                'tag_type'=>$_GET['type']
            ),

            'use_buildin_new_dialog'=>false,
            'use_buildin_set_tag'=>true,
            'use_buildin_recycle'=>false,
            'use_buildin_export'=>true,
            'allow_detail_popup'=> true,
            'use_view_tab' => true,
            'use_buildin_filter'=>true
        );

        /**
         *         $actions_base['allow_detail_popup'] = true;
        $actions_base['use_buildin_set_tag'] = true;
        $actions_base['use_buildin_export'] = true;
        $actions_base['use_buildin_filter'] = true;
        $actions_base['use_view_tab'] = true;
         */
        $this->finder( 'desktop_mdl_tag', $params );
    }

    public function tag_edit( $id ){
       $this->url = 'index.php?app='.$_GET['app'].'&ctl='.$_GET['ctl'].'&app_id='.$_GET['app_id'];
       $render =  app::get('desktop')->render();
       $mdl_tag = app::get('desktop')->model('tag');
       $tag = $mdl_tag->dump($id,'*');
       $this->pagedata['data'] = $tag;
       $this->display('edittag.html');
    }

    public function tag_add()
    {
        if ( $_POST ){
        	$this->begin();
            $obj_tag = app::get( 'desktop' )->model( 'tag' );
            if( empty($_POST['tag_name']) ){
                $this->end( false, app::get('tags')->_('标签名不能为空,保存失败') );
            }

            $arrTags = $obj_tag->getList('tag_name',array('tag_name'=>$_POST['tag_name']) );
            if(!$_POST['tag_id'] && $arrTags){
                $this->end( false, app::get('tags')->_('标签名'.$_POST['tag_name'].'已存在,保存失败') );
            }
            $isSmallTag = 0;
            if(isset($_POST['show_small_tag']) && $_POST['show_small_tag'] == 'on'){
                $isSmallTag = 1;
            }
            $isSmallGoods = 0;
            if(isset($_POST['show_small_tag_goods']) && $_POST['show_small_tag_goods'] == 'on'){
                $isSmallGoods = 1;
            }

            $data = array(
                'tag_id'     => $_POST['tag_id'],
                'tag_name'   => $_POST['tag_name'],
                'tag_abbr'   => $_POST['tag_abbr'],
                'tag_type'   => $_POST['tag_type'],
                'show_small_tag'   => $isSmallTag,
                'show_small_tag_goods'   => $isSmallGoods,
                'app_id'     => $_POST['app_id'],
                'tag_model'  => 'normal',
                'link_switch'  => $_POST['tag_link_switch'],
                'tag_link'  => $_POST['tag_link'],
                'tag_fgcolor'=> $_POST['tag_fgcolor'],
				'tag_bgcolor'=> $_POST['tag_bgcolor'],
                'params'     => $_POST['params'],
                'display_order'=> $_POST['params']['display_order'],
            );
            $obj_tag->save( $data );
            $this->end( true, app::get('tags')->_('保存成功') );
        }
        else {
        	if ( $_GET['type'] ){
        	    $data['tag_type'] = $_GET['type'];
        	    $data['app_id']   = $_GET['app_id'];
        	    $this->pagedata['data'] = $data;
        	}
            $this->page('edittag.html');
        }
    }

    public function add($tag_id, $id=null)
    {
        if($id){
            $goodsTagModel = app::get('b2c')->model('goods_tags');
            $tagInfo = $goodsTagModel->getRow('*', array('id'=>$id));
            if(!empty($tagInfo)){
                $this->pagedata['tag'] = $tagInfo;
            }
        }
        $this->pagedata['tag_id'] = $tag_id;
        $this->display('addgoods.html');
    }
    
    public function save()
    {
        $this->begin();
        $data = $_POST;        
        $goodsTagModel = app::get('b2c')->model('goods_tags');
        $data['id'] = intval($data['id']);
        if($data['id']){
            $goodsTagModel->update($data, array('id'=>$data['id']));            
        } else {
            $goodsTagModel->insert($data);            
        }
        
        $this->end(true, '保存成功');
    }
    
    public function batch_add($tag_id)
    {
        $this->pagedata['finder_id'] = $_GET['finder_id'];
        $this->pagedata['tag_id'] = $tag_id;
        $tagModel = app::get('desktop')->model('tag');
        $tagInfo = $tagModel->getRow('*', array('tag_id'=>$tag_id));
        $this->pagedata['tag'] = $tagInfo;
        $this->display('batch_add_goods.html');
    }

    public function delete(){
        $id = $_GET['id'];
        if($id){
            $this->begin();
            $goodsTagModel = app::get('b2c')->model('goods_tags');
            $res = $goodsTagModel->delete(array('id'=>$id));
            if($res){
                $msg=app::get('tags')->_('关联商品删除成功');
                $this->end(true,$msg);
            }
        }
        $msg=app::get('tags')->_('关联商品删除失败');
        $this->end(false,$msg);
    }

    public function saveGoods()
    {
        $goodsModel = app::get('b2c')->model('goods');
        if(empty($_POST['tag_id'])){
            $msg=app::get('tags')->_('关联商品添加失败');
            echo($msg);exit;
        }
        if(empty($_POST['goods_bns'])){
            $msg=app::get('tags')->_('请输入关联商品的编号');
            echo($msg);exit;
        }
        $bns = explode("\n", trim($_POST['goods_bns']));
        $error_bns = array();//不存在的货号
        $goodsIds = $goodsModel->getList('goods_id,bn', array('bn|in' => array_values($bns)));
        if (empty($goodsIds)) {
            $msg = app::get('tags')->_('商品编号'.$_POST['goods_bns'].'不存在');
            echo($msg);exit;
        }
        
        //检测不存在的货号
        $all_goods_bn = array_column($goodsIds, 'bn');
        foreach($bns as $v){
            if( ! in_array($v, $all_goods_bn)){
                $error_bns[] = $v;
            }
        }
   
        $succ_num = 0;
        $fail_num = 0;
        $exist_num = 0;
        if($goodsIds) {
            $data = array();
            $tag_rel = app::get('desktop')->model('tag_rel');
            foreach ($goodsIds as $val) {                
                $row = $tag_rel->getRow('*',array('tag_id'=>$_POST['tag_id'],'rel_id'=>$val['goods_id'],'tag_type'=>'goods'));
                if(!empty($row)){
                    $exist_num ++;
                    continue;
                }
                $data['tag']['tag_id'] = $_POST['tag_id'];
                $data['tag_type'] = 'goods';
                $data['app_id'] = 'b2c';
                $data['rel_id'] = $val['goods_id'];
                if($tag_rel->save($data)){
                    $succ_num ++;
                } else {
                    $fail_num ++;
                }                
            }
        }
        $msg = "成功添加 {$succ_num} 个商品";
        if( $exist_num > 0){
            $msg .= "，已存在数量: {$exist_num}";
        }
        
        if( $fail_num > 0){
            $msg .= "，失败数量: {$fail_num}";
        }
        
        if($error_bns) {
            $msg .= '，不存在的货号：'.implode(',', $error_bns);
        }
        
        echo($msg);
    }

    public function deleteGoods(){
        $goods_id = $_GET['id'];
        $tag_id = $_GET['tag_id'];
        $this->begin();
        if(empty($tag_id) || empty($goods_id)){
            $msg = app::get('tags')->_('删除失败');
            $this->end(false, $msg);
        }
        $tag_rel = app::get('desktop')->model('tag_rel');
        $tag_rel->delete(array('tag_id'=>$tag_id,'rel_id'=>$goods_id));
        $msg=app::get('tags')->_('删除成功！');
        $this->end(true,$msg);
        //$this->redirect(array('app'=>'b2c','ctl'=>'admin_goods','act'=>'label'));
    }

}
